package org.swift.mashup.engine.deployment;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class MashupDependentsMap extends ConcurrentHashMap<String,MashupDependents> {
    static private final MashupDependentsMap instance = new MashupDependentsMap();

    static public MashupDependentsMap getInstance() {
        return instance;
    }

    private MashupDependentsMap() {}

    public void addDependent(String dependency, long dependencyFiledate, String dependent) {
		MashupDependents md = this.get(dependency, true);
		md.setDependent(dependencyFiledate, dependent);
	}

	public void removeDependent(String dependent) {
		for ( MashupDependents md : this.values() ) {
			md.removeDependent(dependent);
		}
	}

	public MashupDependents get(String mashupName, boolean createIfMissing) {
		MashupDependents md = this.get(mashupName);

		if ( md == null && createIfMissing ) {
			md = new MashupDependents(mashupName);
			this.put(mashupName, md);
		}

		return md;
	}

	public Set<String> getOutOfDateDependents(String mashupName, long date) {
		MashupDependents md = this.get(mashupName);
		return ( md != null ) ? md.getOutOfDateDependents(date) : new HashSet<String>();
	}
}
